﻿using System.Web.Mvc;
using Pay.Core;
using Pay.IService;
using System.Collections.Generic;
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Pay.DTO;

namespace Pay.Api.Controllers {
	public class HomeController : Controller {
		public ActionResult Index() {
			return View();
		}

		[ValidateInput(false)]
		public string sign(string json_content, string private_key) {
			var collection = json_content.ToObject<SortedDictionary<string, object>>();
			string result = string.Empty;
			string merchant_no = string.Empty;
			foreach (var kv in collection) {
				if (kv.Key == "sign") continue;
			//	if (kv.Key == "merchant_no") merchant_no = kv.Value.ToString();
				if (kv.Value is JArray) {
					string str = "";
					var arrs = kv.Value as JArray;
					foreach (var obj in arrs) {
						str += FillStringBuilder(obj.ToObject<SortedDictionary<string, object>>());
					}
					//str = str.Trim('&');
					//result += "&" + kv.Key + "=" + str;
					result += str;
				} else if (kv.Value is JObject) {
					var obj = kv.Value as JObject;
					//result += "&" + kv.Key + "=" + FillStringBuilder(obj.ToObject<SortedDictionary<string, object>>());
					result += FillStringBuilder(obj.ToObject<SortedDictionary<string, object>>());
				} else {
					if (kv.Value != null && !string.IsNullOrEmpty(kv.Value.ToString()))
						result += "&" + kv.Key + "=" + kv.Value;
				}
			}
			result = result.Trim('&');
			var app_sign = RSAFromPkcs.sign(result, private_key, "utf-8");
			return app_sign;
		}

		private string FillStringBuilder(SortedDictionary<string, object> dic) {
			string str = string.Empty;
			foreach (var kv in dic) {
				if (kv.Value != null && !string.IsNullOrEmpty(kv.Value.ToString()))
					str += "&" + kv.Key + "=" + kv.Value;
			}
			return str;
		}

		[ValidateInput(false)]
		public string hfsign(string xml) {
			var collection = new SortedDictionary<string, string>();
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.LoadXml(xml);
			string jsonText = JsonConvert.SerializeXmlNode(xmlDoc);
			JToken jToken = JToken.Parse(jsonText);
			var obj = jToken.Value<JObject>("HFBankData");
			var collection_head = obj["head"].ToObject<Dictionary<string, string>>();
			collection_head.Remove("signature");
			var collection_body = obj["body"].ToObject<Dictionary<string, string>>();
			foreach (var item in collection_head) {
				collection.Add(item.Key, item.Value);
			}
			foreach (var item in collection_body) {
				collection.Add(item.Key, item.Value);
			}
			string result = string.Empty;
			foreach (var kv in collection)
				result += "&" + kv.Key + "=" + kv.Value;
			result = result.Trim('&');

			string _hf_privateKey = "30820275020100300d06092a864886f70d01010105000482025f3082025b020100028181009c94ae9a0d4fc00ae1b6deb8d4c21102c735a78d64c805af42a9f04f55efc83e31847962a9c6b4e525003c16791e48fcb31003d980971b7e342d044ad0ccc8e10a8ad36ba473e1da3dda963690671bba89ef99829706419d30fc1a0dec3c3c1046ebf125cbf43a955befacaf8023d5a777e3b6f97e44ead6606b714a77bbb54902030100010281807deccd26b381e9ae56ac5d1e446cf68547748d1597718db0cf6a200b8dcf533941558ad1a03c600ad0e1097201622bcc9ead488b49713cf9183903f4af4ba258a2f6e1c0ab467e5b8d18705badc087ce6564528a1e9e9e5bd9cceb04419c2552e446dbdf0afb1245c1a712b0b7dff08cb1941699345de20dd35e6ea5707dad09024100a8984601e1f2e2eb24ab91d838cfd10a4ef0cf191f4222c88b3cf7fa8e5c7edf6602b3a1f27b4fabfa2fabd2bb49a308943b64e802397b5875ae0285812b6b6b024100edc1eb57afae84c6e24c516471325f561ff4fd4396172738b41eb7ade86ab76a660e569dff0474f9f962b9afddebf19224bfbda5c225b0edb6254c2af517631b02405229dbbc612460c85e17ab3d27fac79271845439970d412c610736870a4833a0a5f35d1de8b185d3775b322ec20d9201c553e8ca5503348498dd53e7425b29bf02405a3b463db906da8d4045067ec01705e781a95094ddd95ba1417a86eb1151aeda4a71169e4fb23952c53a9a8e0c030763175f4d7223047ae93c3f5b70d223561102405f3af03ef90c86f83b30827fb05abba0d01e2e37de8ba5ae6bd0db3b4c4c0719e5346104701f686cb674a39a8c30630d6c96ebe33efc1e503081a39cdd6be109";
			var app_sign = RSAFromPkcs.sign2Hex(result, _hf_privateKey, "utf-8");
			return app_sign;
		}
	}
}
